package com.macro.mall.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsTodayMorningExpressParam;
import com.macro.mall.mapper.BmsTodayMorningExpressMapper;
import com.macro.mall.model.BmsTodayMorningExpress;
import com.macro.mall.model.BmsTodayMorningExpressExample;
import com.macro.mall.query.BaseQuery;
import com.macro.mall.service.BmsTodayMorningExpressService;
import com.macro.mall.utils.UserUtils;

import cn.hutool.core.date.DateTime;

@Service
public class BmsTodayMorningExpressServiceImpl implements BmsTodayMorningExpressService {

    @Autowired
    private BmsTodayMorningExpressMapper bmsTodayMorningExpressMapper;
    
    @Autowired
    private UserUtils userUtils;
    
    /**
     * 根据id获取今日早报信息
     * 
     * @param id
     * @return
     */
    @Override
    public BmsTodayMorningExpress getOneTodayMorningExpress(String id) {
        return bmsTodayMorningExpressMapper.selectTodayMorningExpressById(id);
    }

    /**
     * 分页获取全部早报信息
     */
    @Override
    public List<BmsTodayMorningExpress> getAllTodayMorningExpress(BmsTodayMorningExpressParam bmsTodayMorningExpressParam) {
        PageHelper.startPage(bmsTodayMorningExpressParam.getPageNum(), bmsTodayMorningExpressParam.getPageSize());
        BmsTodayMorningExpressExample example = new BmsTodayMorningExpressExample();
        BmsTodayMorningExpressExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteNotEqualTo(1);
        criteria.andTitleLike("%" + bmsTodayMorningExpressParam.getTitle() + "%");
        List<BmsTodayMorningExpress> list = bmsTodayMorningExpressMapper.findList(example);
        return list;
    }

    /**
     * 添加今日早报
     */
//    @Override
//    @Transactional
//    public int create(BmsTodayMorningExpressParam bmsTodayMorningExpressParam) {
//        BmsTodayMorningExpress bmsTodayMorningExpress = new BmsTodayMorningExpress();
//        BeanUtils.copyProperties(bmsTodayMorningExpressParam, bmsTodayMorningExpress);
//        Long userId = userUtils.getUserId();
//        bmsTodayMorningExpress.setCreateUser(userId);
//        bmsTodayMorningExpress.setUpdateUser(userId);
//        bmsTodayMorningExpress.setCreateTime(new DateTime());
//        bmsTodayMorningExpress.setUpdateTime(new DateTime());
//        int count = bmsTodayMorningExpressMapper.insertSelective(bmsTodayMorningExpress);
//        return count;
//    }


    @Override
    public int create(BmsTodayMorningExpressParam bmsTodayMorningExpressParam) {
        BmsTodayMorningExpress bmsTodayMorningExpress = new BmsTodayMorningExpress();
        BeanUtils.copyProperties(bmsTodayMorningExpressParam, bmsTodayMorningExpress);
        //获取当前登录人
        Long userId = userUtils.getUserId();
        bmsTodayMorningExpress.setCreateUser(userId);
        bmsTodayMorningExpress.setUpdateTime(new DateTime());
        bmsTodayMorningExpress.setUpdateUser(userId);
        bmsTodayMorningExpress.setCreateTime(new DateTime());
        // Generate UUID for the ID field
        bmsTodayMorningExpress.setId(UUID.randomUUID().toString());
        int count = bmsTodayMorningExpressMapper.insertSelective(bmsTodayMorningExpress);
        return  count;
    }

    /**
     * 删除今日早报
     */
    @Override
    @Transactional
    public int delete(List<UUID> ids) {
        BmsTodayMorningExpress express = new BmsTodayMorningExpress();
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        express.setIsDelete(true);
        express.setUpdateUser(userId);
        express.setUpdateTime(new DateTime());
        
        BmsTodayMorningExpressExample example = new BmsTodayMorningExpressExample();
        example.createCriteria().andIdIn(ids);
        int count = bmsTodayMorningExpressMapper.updateByExampleSelective(express, example);
        return count;
    }

    /**
     * 修改今日早报
     */
    @Override
    @Transactional
//    public int update(BmsTodayMorningExpressParam bmsTodayMorningExpressParam) {
//        BmsTodayMorningExpress express = new BmsTodayMorningExpress();
//        express.setCoverPic(bmsTodayMorningExpressParam.getCoverPic());
//        express.setTitle(bmsTodayMorningExpressParam.getTitle());
//        express.setRemark(bmsTodayMorningExpressParam.getRemark());
//        express.setStatus(bmsTodayMorningExpressParam.getStatus());
//        express.setDescription(bmsTodayMorningExpressParam.getDescription());
//        Long userId = userUtils.getUserId();
//        express.setUpdateUser(userId);
//        express.setUpdateTime(new DateTime());
//        BmsTodayMorningExpressExample example = new BmsTodayMorningExpressExample();
//        example.createCriteria().andIdEqualTo(bmsTodayMorningExpressParam.getId());
//        int count = bmsTodayMorningExpressMapper.updateByExampleSelective(express, example);
//        return count;
//    }
    public int update(BmsTodayMorningExpressParam bmsTodayMorningExpressParam) {
        BmsTodayMorningExpress express = new BmsTodayMorningExpress();
        express.setCoverPic(bmsTodayMorningExpressParam.getCoverPic());
        express.setTitle(bmsTodayMorningExpressParam.getTitle());
        express.setRemark(bmsTodayMorningExpressParam.getRemark());
        express.setStatus(bmsTodayMorningExpressParam.getStatus());
        express.setDescription(bmsTodayMorningExpressParam.getDescription());
        Long userId = userUtils.getUserId();
        express.setUpdateUser(userId);
        express.setUpdateTime(new DateTime());
        BmsTodayMorningExpressExample example = new BmsTodayMorningExpressExample();
        example.createCriteria().andIdEqualTo(bmsTodayMorningExpressParam.getId());
        int count = bmsTodayMorningExpressMapper.updateByExampleSelective(express, example);

        return count;
    }
}
